package com.hezuti.sns.action;


import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

import com.hezuti.common.utils.EncodeUtil;
import com.hezuti.common.utils.StringUtil;
import com.hezuti.sns.domain.Lserial;
import com.hezuti.sns.domain.User;

public class UserAction extends SnsAction {
	private int pid;
	
	private String t;
	
	private  String vstamp;
	
	//上次登录记录
	private Lserial lastLoginInfo;

	public String userinfo(){
		
		if(t==null||"".equals(t.trim())){
			t="common";
		}
		if("headpic".equals(t)){
			
			return "headpic";
		}
		if("password".equals(t)){
			
			return "password";
		}
		if("common".equals(t)){//展示用户普通信息
			
			return "common";
		}
		if("school".equals(t)){//修改用户教育信息
			
			return "school";
		}
		if("company".equals(t)){//修改工作信息
			
			
			return "company";
		}
		if("location".equals(t)){//居住信息
			
			return "location";
		}
		return SUCCESS;
	}
	
	public String home() {
		// TODO 初始化个人首页信息
		user = getUserInfo();
		//TODO 获取 系统推荐信息
		//TODO 其他操作，等业务确定后在定

		lastLoginInfo = userService.getLastLoinInfo(user.getId());
		return SUCCESS;
	}

	/**
	 * 修改用户信息
	 * @return
	 */
	public String editUserInfo(){
		if("password".equals(t)){
			String oldPassword=getRequest().getParameter("oldpassword");
			String password1=getRequest().getParameter("password1");
			String password2=getRequest().getParameter("password2");
			if(!StringUtil.isNull(password1)&&!StringUtil.isNull(password2)&&password1.equals(password2)&&getUserInfo().getPassword().equals(oldPassword)){
				userService.doChangePassword(getUserInfo().getEmail(), oldPassword, password1);	
				user=getUserInfo();
				user.setPassword(password1);
				setUserInfo(user);
			}
		}
		if("info".equals(t)){
			if(user!=null){
				setUserInfo(userService.doModifyUserInfo(user.getName(), user.getGender(), user.getBirthday(), user.getMobile(), user.getEmail(), getUserInfo().getId()));
			}
		}
		return SUCCESS;
	}
	/**
	 * 请求验证邮箱<br/>
	 * 向邮箱发送 验证 url
	 * @return
	 */
	public String provInfo(){
		String msg="false";
		if("email".equals(t)){//像用户邮箱发送验证信息
			try{
			String params=new Date().getTime()+"_"+EncodeUtil.encodeByBASE64((getUserInfo().getId()+"").getBytes());//time_uid
			params=EncodeUtil.encodeByBASE64(params.getBytes());
			String content="<a href=\"http://www.hezuti.com/user/verifyInfo.jspa?t=email&vstamp="+URLEncoder.encode(params,"utf-8")+"\">验证我的邮箱</a>";
			userService.doSendEmail(getUserInfo().getEmail(),"邮箱验证-合租体-hezuti.com",content);
			getResponse().getWriter().println("验证邮件已经发送到您的邮箱: "+getUserInfo().getEmail());
			}catch(Exception e){
				log.error("provInfo : prov email failed ! userEmail :"+getUserInfo().getEmail());
				e.printStackTrace();
			}
			
		}
		if("mobile".equals(t)){
			
		}
		try {
			getResponse().getWriter().println(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 接受邮箱中的验证url
	 * @return
	 */
	public String verifyInfo(){
		String msg="";
		if("email".equals(t)){
			try{
				if(null!=vstamp||!"".equals(vstamp)){
					String tmp=new String(EncodeUtil.decodeByBASE64(vstamp));
					String[] o=tmp.split("_");
					if(isEffective(o[0])&&!StringUtil.isNull(o[1])){
						user=userService.doModifyEmailStatus(Integer.valueOf(new String(EncodeUtil.decodeByBASE64(o[1]))));
						if(user!=null){
							setUserInfo(user);
						}
					}
				}
			}catch(Exception e){
				log.error("verifyInfo ：email vstamp："+vstamp);
				e.printStackTrace();
			}
		}
		
		return SUCCESS;
	}
	
	private boolean isEffective(String string) {
		Long s=Long.valueOf(string);
		Long n=new Date().getTime();
		if((n-s)/(1000*60*60)>24){
			return false;
		}
		return true;
		
	}

	/**
	 * 取的用户所有的产品
	 * 
	 * @return
	 */
	public String getAllProduct() {
		try {
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 绑定 产品
	 * 
	 * @return
	 */
	public String brandProduct() {
		try {
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 注册用户
	 * 
	 * @return
	 */
	public String register() {
		try {
			if (null == user) {
				return ERROR;
			}
			if (isNull(user.getName())) {
				return ERROR;
			}
			if (isNull(user.getEmail())) {
				return ERROR;
			}
			if (isNull(user.getPassword())) {
				return ERROR;
			}
			user = userService.doAddUser(user);
			loginUser(Lserial.LOGIN_TYPE_WEB_EMAIL);
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	public String loginByEmail() {
		try {
			if (null == user)
				return ERROR;
			String email = user.getEmail();
			String pwd = user.getPassword();
			if (null == email || "".equals(email.trim()) || null == pwd
					|| "".equals(pwd.trim()))
				return ERROR;
			user = userService.doCheckPasswordByEmail(email, pwd);
			if (null == user) {
				return ERROR;
			}
			loginUser(Lserial.LOGIN_TYPE_WEB_EMAIL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String backUrl=(String) getSession().getAttribute("backurl");
		if(backUrl!=null){
		try {
			getResponse().sendRedirect(URLDecoder.decode(backUrl,"utf-8"));
			getSession().removeAttribute("backurl");
			return null;
		} catch (Exception e) {
			log.error("redirect failed!! backUrl:"+backUrl);
			e.printStackTrace();
		}
		}
		return SUCCESS;
	}

	public String logout() {
		try {
			logoutUser();
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}

	/**
	 * 手机登录
	 * 
	 * @return
	 */
	public String loginByMobile() {
		try {
			if (null == user)
				return ERROR;
			String mobile = user.getMobile();
			String pwd = user.getPassword();
			if (null == mobile || "".equals(mobile.trim()) || null == pwd
					|| "".equals(pwd.trim()))
				return ERROR;
			user = userService.doCheckPasswordByMobile(mobile, pwd);
			if (null == user) {
				return ERROR;
			}
			loginUser(Lserial.LOGIN_TYPE_WEB_PHONE);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public boolean checkEmail() {
		try {

			// userService.doCheckEmail(u.getEmail());

		} catch (Exception e) {

		}
		return false;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public Lserial getLastLoginInfo() {
		return lastLoginInfo;
	}

	public void setLastLoginInfo(Lserial lastLoginInfo) {
		this.lastLoginInfo = lastLoginInfo;
	}

	public String getT() {
		return t;
	}

	public void setT(String t) {
		this.t = t;
	}

	public String getVstamp() {
		return vstamp;
	}

	public void setVstamp(String vstamp) {
		this.vstamp = vstamp;
	}

	
}
